import Vue from 'vue'
import Router from 'vue-router'
import RouterContainer from "@/components/layout/LayoutContainer.vue"
import {initRouterInterceptor} from "@/interceptor";
import Login from "@/pages/common/login.vue"

Vue.use(Router);
const componentCtx = require.context("@/pages", true, /\.vue$/);
const viewPaths = componentCtx.keys().filter((name) => !name.startsWith("./common"));
const ROUTER_CHILDREN = viewPaths.map((path) => {
    const viewModule = componentCtx(path);
    const {title} = viewModule;
    return {
        path: path.replace(/\.\/(.*)\.vue$/g, "$1"),
        component: r => r(viewModule), meta: {title}, props: (route) => route.query
    };
});
const router = new Router({
    routes: [
        {
            path: '/',
            redirect: ROUTER_CHILDREN[0].path,
            component: RouterContainer,
            children: ROUTER_CHILDREN
        }, {
            path: "/login",
            component: Login,
        }
    ]
});
initRouterInterceptor(router);
export default router;
